﻿using System;
using System.Collections.Generic;
using DynamicJsonParser;
using System.Web.Script.Serialization;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace Easycmd
{
    // DONE  : 记录上一次打开的文件夹组。left right folder group
    //        string demoJson = @"
    //{
    //    'lastopens':
    //    {
    //        'left':['c:\\','c:\\windows'],
    //        'right':['c:\\','c:\\windows']
    //    }
    //}
    //    ";
    public class LastOpens
    {

        public List<string> Left { get; set; }
        public List<string> Right { get; set; }
        public LastOpens()
        {
            Left = new List<string>();
            Right = new List<string>();
            // load 
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            try
            {
                dynamic easycmd = serializer.Deserialize<object>(ConfJson);
                foreach (string str in easycmd.lastopens.left)
                    Left.Add(str);
                foreach (string str in easycmd.lastopens.right)
                    Right.Add(str);
            }
            catch
            {
                Left.Add("c:\\");
                Right.Add("c:\\");
            }
        }
        string AppConf { get { return Path.GetDirectoryName(Application.ExecutablePath) + "\\easycmd.conf"; } }
        public void writeConf(string[] left, string[] right)
        {

            System.IO.File.WriteAllText(AppConf, getJson(left, right));
        }

        private string getJson(string[] left, string[] right)
        {
            // create json
            dynamic easycmdObj = new DynamicJsonObject();
            dynamic lastopensObj = new DynamicJsonObject();
            ArrayList leftObj = new ArrayList();
            ArrayList rightObj = new ArrayList();
            foreach (string str in left)
                leftObj.Add(str);
            foreach (string str in right)
                rightObj.Add(str);
            lastopensObj.left = leftObj;
            lastopensObj.right = rightObj;
            easycmdObj.lastopens = lastopensObj;
            return easycmdObj.ToString();
        }
        public string ConfJson { get {
            try
            {
                return System.IO.File.ReadAllText(AppConf);
            }
            catch { return ""; }
        } }
        // just testcase !
        void a()
        {

            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic easycmd = serializer.Deserialize<object>(ConfJson);
            Console.WriteLine(easycmd.lastopens.left);
            Console.WriteLine(easycmd.lastopens.right);
            Console.WriteLine(easycmd.lastopens.right[0]);

            Console.ReadLine();
            // create json
            dynamic easycmdObj = new DynamicJsonObject();
            dynamic lastopensObj = new DynamicJsonObject();
            ArrayList leftObj = new ArrayList();
            ArrayList rightObj = new ArrayList();
            leftObj.Add("c:\\");
            leftObj.Add("c:\\");
            rightObj.Add("c:\\windows");
            lastopensObj.left = leftObj;
            lastopensObj.right = rightObj;
            easycmdObj.lastopens = lastopensObj;
            Console.WriteLine(easycmdObj.ToString());
            Console.ReadLine();
            return;
        }
    }
}
